package org.example;

import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

public class loginView {
    public loginView() {
        initComponents();
    }

    private void login(ActionEvent e) throws SQLException, ClassNotFoundException, IOException {
        String username = nameField.getText();
        String password = new String(passwordField.getPassword());

         String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象,来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        if (userMapper.login(username,password) == 1) {
            JOptionPane.showMessageDialog(null,"登录成功");
            new helpView();
            loginViewForm.dispose();
            // 登录成功后的操作
        } else {
            JOptionPane.showMessageDialog(null, "用户名或密码错误");
        }
    }

    private void help(ActionEvent e) {
        loginViewForm.dispose();
        new helpView();
    }

    private void initComponents() {
        loginViewForm = new JFrame("loginViewClass");
        loginViewForm.setVisible(true);
        loginViewForm.setSize(400, 300);
        loginViewForm.setResizable(false);
        loginViewForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginViewForm.setLocationRelativeTo(null);

        loginLabel = new JLabel();
        nameLabel = new JLabel();
        passwordLabel = new JLabel();
        nameField = new JTextField();
        passwordField = new JPasswordField();
        loginOrCreateButton = new JButton();
        helpButton = new JButton();

        {
            var loginViewFormContentPane = loginViewForm.getContentPane();
            loginViewFormContentPane.setLayout(null);

            loginLabel.setText("登录界面");
            loginLabel.setFont(loginLabel.getFont().deriveFont(loginLabel.getFont().getSize() + 20f));
            loginViewFormContentPane.add(loginLabel);
            loginLabel.setBounds(130, 15, 140, 40);

            nameLabel.setText("用户名");
            nameLabel.setFont(nameLabel.getFont().deriveFont(nameLabel.getFont().getSize() + 7f));
            loginViewFormContentPane.add(nameLabel);
            nameLabel.setBounds(65, 80, 60, 25);

            passwordLabel.setText("密码");
            passwordLabel.setFont(passwordLabel.getFont().deriveFont(passwordLabel.getFont().getSize() + 7f));
            loginViewFormContentPane.add(passwordLabel);
            passwordLabel.setBounds(80, 120, 40, 25);
            loginViewFormContentPane.add(nameField);
            nameField.setBounds(140, 80, 180, 30);
            loginViewFormContentPane.add(passwordField);
            passwordField.setBounds(140, 120, 180, 30);

            loginOrCreateButton.setText("登录");
            loginOrCreateButton.setFont(loginOrCreateButton.getFont().deriveFont(loginOrCreateButton.getFont().getSize() + 3f));
            loginOrCreateButton.addActionListener(e -> {
                try {
                    login(e);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException | IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            loginViewFormContentPane.add(loginOrCreateButton);
            loginOrCreateButton.setBounds(90, 175, 100, 30);

            helpButton.setText("帮助");
            helpButton.setFont(helpButton.getFont().deriveFont(helpButton.getFont().getSize() + 3f));
            helpButton.addActionListener(e -> help(e));
            loginViewFormContentPane.add(helpButton);
            helpButton.setBounds(210, 175, 100, 30);
        }
    }
    private JFrame loginViewForm;
    private JLabel loginLabel;
    private JLabel nameLabel;
    private JLabel passwordLabel;
    private JTextField nameField;
    private JPasswordField passwordField;
    private JButton loginOrCreateButton;
    private JButton helpButton;

    public static void main(String[] args) {
        new loginView();
    }
}
